class BlogController < ApplicationController
  caches_page :index, :index_by_date
  
  before_filter :get_archives, :add_nav_sections

  def index
    @blog_entries = 
      BlogEntry.get_items(BlogEntry::ITEMS_PER_PAGE, @page, params[:tag])
    @blog_entries_pages = 
      BlogEntry.get_counts(BlogEntry::ITEMS_PER_PAGE, params[:tag])
  end
  
  def index_by_date
    return redirect_to(:action => :index) if params[:year].blank?
    
    date_start, date_end = DateRange.parse(params[:year], params[:month], params[:day])
    
    if !params[:title].blank?
      @blog_entries =
        BlogEntry.find(
          :all,
          :conditions => {
            :canonical_title  => params[:title].to_s,
            :created_at       => date_start .. date_end,
            :published        => true
          },
          :limit      => BlogEntry::ITEMS_PER_PAGE,
          :order      => "created_at ASC"
        )
      
      if @blog_entries.size > 0
        @permalinked = true
        return render(:action => "blog/index")
      end
    end
    
    @blog_entries = 
      BlogEntry.find(
        :all,
        :conditions => {
          :created_at       => date_start .. date_end,
          :published        => true
        },
        :limit      => BlogEntry::ITEMS_PER_PAGE,
        :offset     => (@page - 1) * BlogEntry::ITEMS_PER_PAGE,
        :order      => "created_at ASC"
      )
    @blog_entries_pages = 
      begin
        counts = 
          BlogEntry.count(
            :all,
            :conditions => {
              :created_at       => date_start .. date_end,
              :published        => true
            },
            :limit      => BlogEntry::ITEMS_PER_PAGE,
            :order      => "created_at ASC"
          )
        pages = ((counts - 1)/ BlogEntry::ITEMS_PER_PAGE) + 1
        [counts, pages]
      end
    
    render :action => "blog/index"
  end
  
  def search
    return redirect_to(:action => :index) if params[:q].blank?
    
    @blog_entries = 
      BlogEntry.find(
        :all,
        :conditions => ["MATCH(title,body) AGAINST(?) AND published = 1", params[:q]],
        :limit      => BlogEntry::ITEMS_PER_PAGE,
        :offset     => (@page - 1) * BlogEntry::ITEMS_PER_PAGE
      )
    @blog_entries_pages = 
      begin
        counts = 
          BlogEntry.count(
            :all,
            :conditions => ["MATCH(title,body) AGAINST(?) AND published = 1", params[:q]]
          )
        pages = ((counts - 1)/ BlogEntry::ITEMS_PER_PAGE) + 1
        [counts, pages]
      end
    
    render :action => "blog/index"  
  end
  
  private
    def get_archives
      @archive = {}
      ActiveRecord::Base.connection.execute( "
        SELECT YEAR(created_at) as year, MONTH(created_at) as month, COUNT(*) as count
        FROM blog_entries b 
        WHERE published = 1
        GROUP BY YEAR(created_at), MONTH(created_at);
      " ).each_hash do |result|
        year = result["year"].to_i
        month = result["month"].to_i
        count = result["count"].to_i
        @archive[year] = {"total" => 0, "months" => {}} unless @archive.has_key?(year)
        @archive[year]["total"] += count
        @archive[year]["months"][month] = count
      end
    end
  
    def add_nav_sections
      @nav_sections << ["archives", {:type => "blog"}]
      @nav_sections << ["comments", {}]
    end
end
